package les

class EventoController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def index = {
		if (!session.usuarioLogado) {
			redirect(uri: '/usuario/login.gsp')
			return
		}

		redirect(action: "list", params: params)
	}

	def list = {
		if (!session.usuarioLogado) {
			redirect(uri: '/usuario/login.gsp')
			return
		}
		if (!(session.usuarioLogado?.tipo in ['Jota', 'Coordenador'])) {
			redirect(uri: '/index.gsp')
			return
		}

		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[eventoInstanceList: Evento.list(params), eventoInstanceTotal: Evento.count()]
	}

	def create = {
		if (!session.usuarioLogado) {
			redirect(uri: '/usuario/login.gsp')
			return
		}
		if (!(session.usuarioLogado?.tipo in ['Jota'])) {
			redirect(uri: '/index.gsp')
			return
		}
		
		def eventoInstance = new Evento()
		eventoInstance.properties = params
		return [eventoInstance: eventoInstance]
	}

	def save = {
		if (!session.usuarioLogado) {
			redirect(uri: '/usuario/login.gsp')
			return
		}
		if (!(session.usuarioLogado?.tipo in ['Jota'])) {
			redirect(uri: '/index.gsp')
			return
		}

		def eventoInstance = new Evento(params)
		if (eventoInstance.save(flush: true)) {
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'evento.label', default: 'Evento'), eventoInstance.id])}"
			redirect(action: "show", id: eventoInstance.id)
		}
		else {
			render(view: "create", model: [eventoInstance: eventoInstance])
		}
	}

	def ehDataValida = {
		Date data1 = new Date()
		String dataString = params.dataEHora

		int dia = Integer.parseInt(dataString.substring(8, 10))
		System.out.println(dia)
		String mes = dataString.substring(4, 7)
		System.out.println(mes)
		int ano = (Integer.parseInt(dataString.substring(dataString.size()-4, dataString.size())))
		System.out.println(ano)

		Boolean anoBisexto = new GregorianCalendar().isLeapYear(ano);

		ArrayList<String> mesesTrinta = new ArrayList<String>()

		mesesTrinta.add("Apr")
		mesesTrinta.add("Jun")
		mesesTrinta.add("Sep")
		mesesTrinta.add("Nov")

		if (mesesTrinta.contains("mes") && dia > 30) return false
		if(mes == "Feb"){
			if (dia > 28 && !anoBisexto) return false
			if(dia > 29 && anoBisexto) return false
		}
		return true
	}



	def show = {
		if (!session.usuarioLogado) {
			redirect(uri: '/usuario/login.gsp')
			return
		}
		if (!(session.usuarioLogado?.tipo in ['Jota', 'Coordenador'])) {
			redirect(uri: '/index.gsp')
			return
		}

		def eventoInstance = Evento.get(params.id)
		if (!eventoInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'evento.label', default: 'Evento'), params.id])}"
			redirect(action: "list")
		}
		else {
			[eventoInstance: eventoInstance]
		}
	}

	def edit = {
		if (!session.usuarioLogado) {
			redirect(uri: '/usuario/login.gsp')
			return
		}
		if (!(session.usuarioLogado?.tipo in ['Jota'])) {
			redirect(uri: '/index.gsp')
			return
		}

		def eventoInstance = Evento.get(params.id)
		if (!eventoInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'evento.label', default: 'Evento'), params.id])}"
			redirect(action: "list")
		}
		else {
			return [eventoInstance: eventoInstance]
		}
	}

	def update = {
		if (!session.usuarioLogado) {
			redirect(uri: '/usuario/login.gsp')
			return
		}
		if (!(session.usuarioLogado?.tipo in ['Jota'])) {
			redirect(uri: '/index.gsp')
			return
		}

		def eventoInstance = Evento.get(params.id)
		if (eventoInstance) {
			if (params.version) {
				def version = params.version.toLong()
				if (eventoInstance.version > version) {

					eventoInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'evento.label', default: 'Evento')] as Object[], "Another user has updated this Evento while you were editing")
					render(view: "edit", model: [eventoInstance: eventoInstance])
					return
				}
			}
			eventoInstance.properties = params
			if (!eventoInstance.hasErrors() && eventoInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.updated.message', args: [message(code: 'evento.label', default: 'Evento'), eventoInstance.id])}"
				redirect(action: "show", id: eventoInstance.id)
			}
			else {
				render(view: "edit", model: [eventoInstance: eventoInstance])
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'evento.label', default: 'Evento'), params.id])}"
			redirect(action: "list")
		}
	}

	def delete = {
		if (!session.usuarioLogado) {
			redirect(uri: '/usuario/login.gsp')
			return
		}
		if (!(session.usuarioLogado?.tipo in ['Jota'])) {
			redirect(uri: '/index.gsp')
			return
		}

		def eventoInstance = Evento.get(params.id)
		if (eventoInstance) {
			try {
				eventoInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'evento.label', default: 'Evento'), params.id])}"
				redirect(action: "list")
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'evento.label', default: 'Evento'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'evento.label', default: 'Evento'), params.id])}"
			redirect(action: "list")
		}
	}

	def calendario = {
		if(session.calendarLoaded){
			session.calendarLoaded = false
		} else {
			session.calendarioResult = null
		}
	}

	def pesquisarEventos = {
		Date data1 = new Date()
		Date data2 = new Date()

		String dataString1 = null
		String dataString2 = null

		if(params.data1 == null){
			Date hoje = new Date()
			dataString1 = hoje.toString()
		} else {
			dataString1 = params.data1
		}
		
		if(params.data2 == null){
			Date hoje = new Date()
			dataString2 = hoje.toString()
		} else {
			dataString2 = params.data2
		}

		HashMap<String, Integer> mes = new HashMap<String, Integer>()

		mes.put("Jan", 0)
		mes.put("Feb", 1)
		mes.put("Mar", 2)
		mes.put("Apr", 3)
		mes.put("May", 4)
		mes.put("Jun", 5)
		mes.put("Jul", 6)
		mes.put("Aug", 7)
		mes.put("Sep", 8)
		mes.put("Oct", 9)
		mes.put("Nov", 10)
		mes.put("Dec", 11)
		
				
		data1.date = Integer.parseInt(dataString1.substring(8, 10))
		data2.date = Integer.parseInt(dataString2.substring(8, 10))
		
		data1.month = mes.get(dataString1.substring(4, 7))
		data2.month = mes.get(dataString2.substring(4, 7))
		
		data1.year = (Integer.parseInt(dataString1.substring(dataString1.size()-4, dataString1.size())) - 1900)
		data2.year = (Integer.parseInt(dataString2.substring(dataString2.size()-4, dataString2.size())) - 1900)
		
		data1.hours = 0
		data2.hours = 23
		
		data1.minutes = 0
		data2.minutes = 59
		
		session.calendarioResult = Evento.findAllByDataEHoraBetween(data1, data2)
		session.calendarLoaded = true 
		redirect(action : "calendario")
	}
	
	def imprimirCalendario = {
		
	}
}
